//2009/07/25 18:20:21
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

class VendingMachine
{
public:
    int motorUse(vector <string> prices, vector <string> purchases)
    {
		N = prices[0].size();
		int workout = 0;
        toPrice(prices);
		int lasttime = 0;
        for (int i=0; i<purchases.size(); i++)
        {
			string s;
			int row, column, time;
			// Format transfer;
			for(int j=0, cnt=0; j<purchases[i].size(); j++)
			{
				if(purchases[i][j] == ',' || purchases[i][j] == ':')
				{
					cnt ++;
					s.clear();
				}
				else
					s += purchases[i][j];
				if(cnt == 0)
					row = convertStringToInteger(s);
				else if(cnt == 1)
					column = convertStringToInteger(s);
				else if(cnt == 2)
					time = convertStringToInteger(s);
			}
        }
		return workout;
    }
private:
    int price[50][50];			// Store price information;
	int mostexp[50];			// The highest price in every column;
	int expensive;				// The most expensive column location;
	int N;						// The size of columns;
    void toPrice(vector<string> &p)		//Initiate information;
    {
		expensive = 0;
		memset(mostexp, 0, sizeof(mostexp));
        for (int i=0; i<p.size(); i++)
        {
            for (int j=0; j<p[0].size(); j++)
            {
                price[i][j] = p[i][j];
				mostexp[j] = max(mostexp[j], price[i][j]);
            }
        }
		int mmax=0;
		for(int i=0; i<p[0].size(); i++)
		{
			if(mmax < mostexp[i])
			{
				expensive = i;
				mmax = mostexp[i];
			}
		}
    }
	
	int toMostExpensiveColumn(int col)
	{
		int mmax = mostexp[expensive];
		int index;
		for(int i=0; i<N; i++)
		{
			if(mostexp[i] > mmax)
			{
				index = i;
				mmax = mostexp[i]
			}
		}
		int value = abs(index - expensive);
		expensive = index;
		if(2*value < N)
		{
			return value;
		}
		else
		{
			return N-value;
		}
	}
    int convertStringToInteger(string s)
    {
        stringstream ss(s);
        int number;
        ss >> number;
        return number;
    }
};
/*
 * Despite the length of this problem, it is actually relatively simple to code. 
 * Basically, you just have to follow all of the instructions to the letter, and you will be fine.
 * The first thing to do is parse the prices input into some structure like a 2-D array. 
 * Then, since the purchases are given to you in order, it is pretty easy to apply them iteratively. 
 * For each purchase, if it is 5 or more minutes after the previous one, 
 * rotate to the most expensive column before applying the purchase. 
 * Then rotate to the column of the purchase item, and set t he price of the purchased item to 0. 
 * As you are buying items, if someone tries to buy an item whose price is 0, return -1. 
 * the rotations is relatively simple also. You don't actually need to rotate anything, 
 * just keep an int representing which column is facing out. Then, to get from column a to column b, 
 * you need either abs(a-b) seconds of rotation, or else numColumns - abs(a-b) seconds of rotation, whichever is less. 
 * Finding the most expensive column is also pretty straightforward. 
 * So, none of the components of this problem were really very difficulty, 
 * but putting things all together could be a little tricky, 
 * just because there are a lot of things going on. 
 * But basically, if you didn't have any bugs, 
 * there wasn't much here that was algorithmically difficult.
 * */
